package com.dzeshko;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.dzeshko.auth.GplusAuthHelper;
import com.dzeshko.content.FeedContentManager;
import com.dzeshko.http.HttpRequestBuilder;
import com.dzeshko.http.HttpService;

public class MainFeedActivity extends Activity {
	
	Button btnToSections, btnRefresh, btnSignIn;
	ListView lvFeed;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_feed);
		
		btnToSections = (Button)findViewById(R.id.btnToSections);
		btnRefresh = (Button)findViewById(R.id.btnRefresh);
		btnSignIn = (Button)findViewById(R.id.btnSignIn);
		lvFeed = (ListView)findViewById(R.id.lvFeed);
		
		/* go to GplusAuthHelper.exit, correct and use
		 * 
		 */
		if(GplusAuthHelper.isLogged(this)) {
			btnSignIn.setText("Sign out");
			
			Intent intent = new Intent(this, HttpService.class);
	    	HttpRequestBuilder httpRequestBuilder = new HttpRequestBuilder();
	    	httpRequestBuilder.setType(HttpRequestBuilder.Type.GET);
	    	httpRequestBuilder.setUrl(FeedContentManager.FEED_URL);											//
			intent.putExtra(HttpService.HTTP_REQUEST_BUILDER, httpRequestBuilder);
			startService(intent);
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main_feed, menu);
		return true;
	}
	
	public void onCreateClick(View v) {
    	// Goto SectionsActivity
    	Intent intent = new Intent(this, SectionsActivity.class);
    	startActivity(intent);
    	
    }
	
	public void onClickSignIn(View v){
		//Goto AuthActivity
		if(GplusAuthHelper.isLogged(this)){
			GplusAuthHelper.exit(this);
			btnSignIn.setText("Sign in");
		}
		Intent intent = new Intent(this, AuthActivity.class);
		startActivityForResult(intent, 1);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		//super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_OK){
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
			SharedPreferences token = getSharedPreferences(GplusAuthHelper.AUTH_PREFS,MODE_PRIVATE);
			adapter.add(token.getString(GplusAuthHelper.ACCESS_TOKEN, "").toString());
			adapter.add("1");
			adapter.add("2");
			lvFeed.setAdapter(adapter);
			btnSignIn.setText("Sign out");
		}
	}

	
}
